package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.SysFileShareReq;
import com.ruoyi.system.domain.vo.SysFileShareResp;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysFileShare;
import com.ruoyi.system.service.ISysFileShareService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源共享Controller
 * 
 * @author ruoyi
 * @date 2025-05-02
 */
@RestController
@RequestMapping("/system/share")
public class SysFileShareController extends BaseController
{
    @Autowired
    private ISysFileShareService sysFileShareService;

    /**
     * 查询资源共享列表
     */
    @PreAuthorize("@ss.hasPermi('system:share:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFileShareReq sysFileShare)
    {
        startPage();
        List<SysFileShareResp> list = sysFileShareService.selectSysFileShareList(sysFileShare);
        return getDataTable(list);
    }

    /**
     * 导出资源共享列表
     */
    @PreAuthorize("@ss.hasPermi('system:share:export')")
    @Log(title = "资源共享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFileShareReq sysFileShare)
    {
        List<SysFileShareResp> list = sysFileShareService.selectSysFileShareList(sysFileShare);
        ExcelUtil<SysFileShareResp> util = new ExcelUtil<SysFileShareResp>(SysFileShareResp.class);
        util.exportExcel(response, list, "资源共享数据");
    }

    /**
     * 获取资源共享详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:share:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysFileShareService.selectSysFileShareById(id));
    }

    /**
     * 新增资源共享
     */
    @PreAuthorize("@ss.hasPermi('system:share:add')")
    @Log(title = "资源共享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFileShare sysFileShare)
    {
        return toAjax(sysFileShareService.insertSysFileShare(sysFileShare));
    }

    /**
     * 修改资源共享
     */
    @PreAuthorize("@ss.hasPermi('system:share:edit')")
    @Log(title = "资源共享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFileShare sysFileShare)
    {
        return toAjax(sysFileShareService.updateSysFileShare(sysFileShare));
    }

    /**
     * 删除资源共享
     */
    @PreAuthorize("@ss.hasPermi('system:share:remove')")
    @Log(title = "资源共享", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysFileShareService.deleteSysFileShareByIds(ids));
    }

    @GetMapping("/front/list")
    public TableDataInfo frontList(SysFileShareReq sysFileShare)
    {
        startPage();
        List<SysFileShareResp> list = sysFileShareService.selectSysFileShareList(sysFileShare);
        return getDataTable(list);
    }

    @PostMapping("/front/add")
    public AjaxResult frontAdd(@RequestBody SysFileShare sysFileShare)
    {
        return toAjax(sysFileShareService.insertSysFileShare(sysFileShare));
    }

    @DeleteMapping("/front/delete/{id}")
    public AjaxResult frontRemove(@PathVariable Long id)
    {
        return toAjax(sysFileShareService.deleteSysFileShareByIds(Collections.singletonList(id).toArray(new Long[0])));
    }

    @PostMapping("/front/download/{id}")
    public AjaxResult frontDownload(@PathVariable("id") Long id)
    {
       return success(sysFileShareService.frontDownload(id));
    }
}
